import request from '../utils/request'

// 区域相关接口
export const regionApi = {
  // 获取区域列表
  getRegions: (regionIds: number[]) => {
    const params = new URLSearchParams()
    regionIds.forEach(id => params.append('regionIds', id.toString()))
    return request({
      url: `/region?${params.toString()}`,
      method: 'GET'
    })
  }
}

// 餐桌相关接口
export const boardApi = {
  // 获取餐桌列表
  getBoards: (regionIds: number[]) => {
    const params = new URLSearchParams()
    regionIds.forEach(id => params.append('regionIds', id.toString()))
    return request({
      url: `/board?${params.toString()}`,
      method: 'GET'
    })
  },

  // 添加餐桌
  addBoard: (data: {
    boardName: string;
    seat: number;
    boardType: string;
    boardStatus: string;
    regionId: number;
  }) => {
    return request({
      url: '/board/add',
      method: 'POST',
      data
    })
  },

  // 更新餐桌
  updateBoard: (data: {
    boardId: number;
    boardName: string;
    seat: number;
    boardType: string;
    boardStatus: string;
    regionId: number;
  }) => {
    return request({
      url: '/board/upd',
      method: 'POST',
      data
    })
  },

  // 删除餐桌
  deleteBoard: (boardId: number) => {
    return request({
      url: '/board/del',
      method: 'POST',
      params: { boardId: boardId.toString() }
    })
  }
}

// 订单相关接口
export const orderApi = {
  // 获取区域订单列表
  getRegionOrders: (regionIds: number[]) => {
    const params = new URLSearchParams()
    regionIds.forEach(id => params.append('regionIds', id.toString()))
    return request({
      url: `/orders/region?${params.toString()}`,
      method: 'GET'
    })
  },

  // 获取订单详情
  getOrderItems: (orderId: number) => {
    return request({
      url: `/orders/${orderId}/items`,
      method: 'GET'
    })
  }
}

// 用户相关接口
export const userApi = {
  // 获取用户列表
  getUsers: (params: {
    startTime?: string;
    endTime?: string;
    uName?: string;
  }) => {
    return request({
      url: '/users/time-range',
      method: 'GET',
      params
    })
  },

  // 批量注销用户
  batchDeleteUsers: (userIds: number[]) => {
    return request({
      url: '/users',
      method: 'DELETE',
      data: userIds
    })
  },

  // 更新用户状态
  updateUserStatus: (uid: number, userData: any) => {
    return request({
      url: `/users/${uid}`,
      method: 'PUT',
      data: userData
    })
  }
}

// 角色相关接口
export const roleApi = {
  // 获取角色列表
  getRoles: () => {
    return request({
      url: '/roles',
      method: 'GET'
    })
  },

  // 删除角色
  deleteRole: (roleId: number) => {
    return request({
      url: `/roles/del/${roleId}`,
      method: 'DELETE'
    })
  },

  // 更新角色
  updateRole: (roleId: number, roleData: any) => {
    return request({
      url: `/roles/upd/${roleId}`,
      method: 'POST',
      data: roleData
    })
  },

  // 添加角色
  addRole: (roleData: any) => {
    return request({
      url: '/roles/add',
      method: 'POST',
      data: roleData
    })
  },

  // 获取用户角色映射
  getUserRoles: () => {
    return request({
      url: '/roles/user-roles',
      method: 'GET'
    })
  },

  // 分配角色给用户
  assignRole: (userId: number, roleId: number) => {
    return request({
      url: '/roles/giveRoles',
      method: 'POST',
      params: { uId: userId.toString(), rId: roleId.toString() }
    })
  },

  // 从用户移除角色
  removeRole: (userId: number, roleId: number) => {
    return request({
      url: '/roles/delRoles',
      method: 'POST',
      params: { uId: userId.toString(), rId: roleId.toString() }
    })
  }
}

// 权限相关接口
export const permissionApi = {
  // 获取权限列表
  getPermissions: () => {
    return request({
      url: '/permissions',
      method: 'GET'
    })
  },

  // 添加角色权限
  addRolePermission: (roleId: number, permissionId: number) => {
    return request({
      url: '/roles_permissions/add',
      method: 'POST',
      params: {
        rId: roleId.toString(),
        pId: permissionId.toString()
      }
    })
  },

  // 删除角色权限
  removeRolePermission: (roleId: number, permissionId: number) => {
    return request({
      url: '/roles_permissions/del',
      method: 'POST',
      params: {
        rId: roleId.toString(),
        pId: permissionId.toString()
      }
    })
  },

  // 获取角色权限关联
  getRolePermissions: () => {
    return request({
      url: '/roles_permissions',
      method: 'GET'
    })
  }
}

// 菜品类型相关接口
export const dishTypeApi = {
  // 获取菜品类型列表
  getDishTypes: () => {
    return request({
      url: '/dishes/types/obj',
      method: 'GET',
      params: {
        t: new Date().getTime().toString()
      }
    })
  },

  // 添加菜品类型
  addDishType: (data: { dtName: string }) => {
    return request({
      url: '/dishes/types/add',
      method: 'POST',
      params: {
        dt: data.dtName
      }
    })
  },

  // 更新菜品类型
  updateDishType: (data: { dtId: number; dtName: string }) => {
    return request({
      url: '/dishes/types/upd',
      method: 'POST',
      params: {
        dt: data.dtId.toString(),
        name: data.dtName
      }
    })
  },

  // 删除菜品类型
  deleteDishType: (dtId: number) => {
    return request({
      url: '/dishes/types/del',
      method: 'GET',
      params: { dtId: dtId.toString() }
    })
  }
}

// 菜品信息相关接口
export const dishApi = {
  // 获取所有菜品
  getAllDishes: () => {
    return request({
      url: '/dishes/allDish',
      method: 'GET'
    })
  },

  // 创建菜品
  createDish: (data: {
    dishImage: string;
    dishName: string;
    price: number;
    dishStatus: number;
    dtId: number;
    dishDescribe: string;
  }) => {
    return request({
      url: '/dishes',
      method: 'POST',
      data
    })
  },

  // 更新菜品
  updateDish: (dishId: number, data: {
    dishImage: string;
    dishName: string;
    price: number;
    dishStatus: number;
    dtId: number;
    dishDescribe: string;
  }) => {
    return request({
      url: `/dishes/${dishId}`,
      method: 'PUT',
      data
    })
  },

  // 获取菜品详情
  getDishInfo: (dishId: number) => {
    return request({
      url: `/dishes/${dishId}`,
      method: 'GET'
    })
  },

  // 更新菜品状态
  updateDishStatus: (dishId: number, status: number) => {
    return request({
      url: `/dishes/${dishId}/status`,
      method: 'PUT',
      params: { status: status.toString() }
    })
  }
}

// 货物订单相关接口
export const cargoOrderApi = {
  // 获取所有货物订单
  getAllCargoOrders: () => {
    return request({
      url: '/cargo/all',
      method: 'GET'
    })
  },

  // 获取订单详情
  getCargoOrderDetails: (orderId: number) => {
    return request({
      url: `/cargo/one?oId=${orderId}`,
      method: 'GET'
    })
  },

  // 入库（增加库存）
  stockIn: (cargoId: number, quantity: number) => {
    return request({
      url: '/cargo/addCargo',
      method: 'GET',
      params: { cargoId: cargoId.toString(), quantity: quantity.toString() }
    })
  },

  // 出库（减少库存）
  stockOut: (cargoId: number, quantity: number) => {
    return request({
      url: '/cargo/delCargo',
      method: 'GET',
      params: { cargoId: cargoId.toString(), quantity: quantity.toString() }
    })
  }
}

// 仓库管理相关接口
export const warehouseApi = {
  // 获取所有仓库信息
  getAllWarehouse: () => {
    return request({
      url: '/warehouse/all',
      method: 'GET'
    })
  },
  // 入库
  stockIn: (cargoId: number, quantity: number) => {
    return request({
      url: '/warehouse/plusOne',
      method: 'GET',
      params: {
        cargoId: cargoId.toString(),
        quantity: quantity.toString()
      }
    })
  },
  // 出库
  stockOut: (cargoId: number, quantity: number) => {
    return request({
      url: '/warehouse/minusOne',
      method: 'GET',
      params: {
        cargoId: cargoId.toString(),
        quantity: quantity.toString()
      }
    })
  }
}

// 货物相关API
export const cargoApi = {
  // 获取所有货物
  getAllCargo: () => {
    return request({
      url: '/cargo/allCargo',
      method: 'GET'
    })
  }
}